<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/6
 * Time: 13:55
 */

namespace app\admin\model;


class PropExchange extends DataBase
{

    public function IndexData()
    {
        $where = [];
        $key = input('key');
        $value = input('value');
        if(!empty($key) && !empty($value)){
        }
        $order = input('sort','id').' '.input('order','asc');
        $page_config = array(
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',10)
        );
        $data =  $this->where($where)->order($order)->paginate($page_config);

        foreach($data as $key => $value){
            $prop_ids = unserialize($value['prop_ids']);
            $str = '';
            foreach($prop_ids as $k => $v){
                $prop_name = Prop::where('id',$v['prop_id'])->value('prop_name');
                $prop_num = $v['num'];
                $str.= $prop_name.':'.$prop_num.'个；';
            }
            $data[$key]['prop_ids'] = $str;
            $data[$key]['type'] = $value['type'];
            $data[$key]['state'] = $value['state'];
            $data[$key]['status'] = $value['status'];

        }
        return $data;
    }

    //兑换道具添加
    public function propExchangeAdd()
    {
        $postData = input('post.');
        $prop_id = $postData['prop_id'];  //道具id集合
        $prop_num = $postData['prop_num']; //道具数量
        if($postData['state'] < 1) return ['code'=>-100,'msg'=>'兑换的钻石不能小于1'];
        $prop_id = array_unique($prop_id); //去重
        $length = count(array_filter($prop_id)); //去空的总长度
        if($length == 0)  return ['code'=>-100,'msg'=>'兑换条件不能为空'];
        if( $length != count(array_filter($prop_num)) ) return ['code'=>-100,'msg'=>'所需数量为空或是道具有重复'];

        $prop_ids = [];
        for($i=0;$i<$length;$i++){
            $arr['prop_id'] = (int)$prop_id[$i];  //该做法json字符串超出编码长度
            $arr['num'] = (int)$prop_num[$i];
            $prop_info = Prop::where(['id'=> $prop_id[$i] ])->find();
            $arr['prop_name'] =$prop_info['prop_name'];
            $arr['thumb_pic'] =$prop_info['thumb_pic'];
            $prop_ids[] = $arr;
        }
       $newData['prop_ids'] = serialize($prop_ids);
      // $newData['prop_ids'] = json_encode($prop_ids,JSON_UNESCAPED_UNICODE);
        $newData['type'] = $postData['type'];
        $newData['state'] = $postData['state'];
        $newData['status'] = $postData['status'];
        $newData['exchange_pic'] = oosUpload($_FILES['exchange_pic']);
        $newData['bg_pic'] = oosUpload($_FILES['bg_pic']);
        $newData['create_time'] = time();
        $res = $this->save($newData);
        if($res) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];

    }

    public function changeStatus()
    {
        $id = input('id');
        $status = $this->where('id',$id)->value('status');
        $status = $status == 1 ? 2 : 1;
        $res =  $this->save(['status'=>$status],['id'=>$id]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

}